﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Order;
using System.Windows.Forms;

namespace OrderWindow
{
    public partial class Form1 : Form
    {
        private OrderService myservice ;
        private int searchway = 0;
        public Form1()
        {
            InitializeComponent();
            List<OrderDetail> details1 = new List<OrderDetail>()
            {
                new OrderDetail(101,"手机",1,1000),
                new OrderDetail(102,"充电器",3,10)
            };//总价1030
            List<OrderDetail> details2 = new List<OrderDetail>()
            {
                new OrderDetail(201,"电脑",1,3000),
                new OrderDetail(202,"路由器",2,70)
            };//总价3140
            List<OrderDetail> details3 = new List<OrderDetail>()
            {
                new OrderDetail(201,"电脑",1,3000),
                new OrderDetail(202,"音响",4,50)
            };//总价3200

            List<Order.Order> myorders = new List<Order.Order>()
           {
               new Order.Order(1,"zhang",details1),
               new Order.Order(2,"wang",details2),
               new Order.Order(3,"li",details3),
           };
            myservice = new OrderService(myorders);
            orderbindingSource.DataSource = myservice.orders;
        }

        private void creat_order_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.ShowDialog();
            myservice.AddOrder(form2.NewOrder);
            orderbindingSource.ResetBindings(false);
        }

        private void update_order_Click(object sender, EventArgs e)
        {
            Order.Order thisorder = orderbindingSource.Current as Order.Order;
            Form3 form3 = new Form3(thisorder);
            form3.ShowDialog();
            orderbindingSource.Remove(orderbindingSource.Current);
            orderbindingSource.Add(form3.UpdateOrder);
            //orderbindingSource.Current = form3.UpdateOrder;
            orderbindingSource.ResetBindings(false);
        }

        private void orderbindingSource_CurrentChanged(object sender, EventArgs e)
        {
            itembindingSource.DataSource = orderbindingSource.Current;
        }

        private void remove_order_Click(object sender, EventArgs e)
        {
            Order.Order thisorder = orderbindingSource.Current as Order.Order;
            myservice.DeleteOrder(thisorder.OrderID);
            orderbindingSource.ResetBindings(false);
        }

        private void search_order_Click(object sender, EventArgs e)
        {
            List<Order.Order> result = new List<Order.Order>();
            if (searchway == 1)
            {
                result = myservice.SearchOrder(Convert.ToInt32(textBox1.Text));
            }
            else if (searchway == 2)
            {
                result = myservice.SearchOrder("CustomerID", textBox1.Text);
            }
            else if (searchway == 3)
            {
                result = myservice.SearchOrder("ProductID", textBox1.Text);
            }
            Form4 form4 = new Form4(result);
            form4.ShowDialog();

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            searchway = 1;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            searchway = 2;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            searchway = 3;
        }
    }
}
